import os
import sys
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/' + '../..'))

import calendar
import numpy as np
from numpy.testing import assert_allclose
import pandas as pd

from warnings import simplefilter
simplefilter(action="ignore", category=pd.errors.PerformanceWarning)


def test_consensus_error_statistics():
    # Test whether the manually constructed
    # consensus forecats line up with the data
    # provided by the Philadelphia Fed
    file_name_philly = '../construct_datasets/input/data_spf_error_statistics_pgdp_3_aic.xls'
    file_name_manual = '../construct_datasets/output/consensus_dataset/consensus_dataset.csv'

    df_manual = pd.read_csv(file_name_manual)
    df_manual.index = df_manual['DATE']
    df_philly = pd.read_excel(file_name_philly, index_col = 0)

    vars_list = ['SPFfor_Step{}'.format(ii) for ii in range(1, 5 + 1)]
    vars_list = vars_list + ['Realiz1']
    test = np.abs(df_manual.loc[:, vars_list] - df_philly.loc[:, vars_list]).max().max()

    assert_allclose(test, 0, atol = 1e-3) # Fails to match exactly because of rounding in original data


def test_individual_error_statistics():
    # Test whether the manually constructed
    # individual forecasts up with the data
    # provided by the Philadelphia Fed. This is a sanity check
    # because by construction the match will not be perfect
    file_name_philly = '../construct_datasets/input/data_spf_error_statistics_pgdp_3_aic.xls'
    file_name_manual = '../construct_datasets/output/consensus_dataset/consensus_dataset.csv'

    df_manual = pd.read_csv(file_name_manual)
    df_manual = df_manual.groupby('DATE').median().reset_index()
    df_manual.index = df_manual['DATE']
    df_philly = pd.read_excel(file_name_philly)
    df_philly = pd.read_excel(file_name_philly, index_col = 0)

    vars_list = ['SPFfor_Step{}'.format(ii) for ii in range(1, 5 + 1)]
    vars_list = vars_list + ['Realiz1']
    test = np.abs(df_manual.loc[:, vars_list] - df_philly.loc[:, vars_list]).max()['SPFfor_Step2']

    assert_allclose(test, 0, atol = 1) # It will not match exactly because data is constructed differently